const userService = require("../server/user");
const jwt = require("jsonwebtoken");
const { JWT_SECRET } = require("../config");
module.exports = class userController {
  static async login(ctx) {
    try {
      const { phone, password } = ctx.request.body;
      const data = await userService.login(phone, password);
      const token = jwt.sign(
        {
          name: phone,
        },
        JWT_SECRET, // secret
        {
          expiresIn: 60 * 60,
        } // 60 * 60 s
      );
      ctx.body = {
        status: 200,
        data: {
          ...data[0],
          token,
        },
        msg: "登入成功！！",
      };
    } catch (err) {
      ctx.body = {
        status: 403,
        msg: err,
      };
    }
  }
  static async register(ctx) {
    try {
      console.log(ctx.request.body);
      const { phone, password } = ctx.request.body;
      const data = await userService.register(phone, password);
      ctx.body = {
        status: 200,
        data,
        message: "注册成功",
      };
    } catch (err) {
      ctx.body = {
        status: 200,
        message: err.message,
      };
    }
  }
};
